package com.ssm.springboot.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.springboot.dao.TbUserDao;
import com.ssm.springboot.dao.TbUserRoleDao;
import com.ssm.springboot.pojo.DataInfo;
import com.ssm.springboot.pojo.TbUser;
import com.ssm.springboot.pojo.req.TbUserReq;
import com.ssm.springboot.service.TbUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * (TbUserReq)表服务实现类
 *
 * @author makejava
 * @since 2021-12-27 20:36:33
 */
@Service
public class TbUserServiceImpl implements TbUserService {
    @Resource
    private TbUserDao tbUserDao;
    @Autowired
    private TbUserRoleDao tbUserRoleDao;

    @Override
    public DataInfo login(TbUserReq tbUserReq, HttpSession session) {
        String captcha = (String) session.getAttribute("captcha");
        if (!captcha.equalsIgnoreCase(tbUserReq.getKaptcha())) {
            return new DataInfo(1, "抱歉,验证码输入错误", null, null);
        }
//        TbUser user = tbUserDao.findByUserName(tbUserReq.getUserName());
//        if (!user.getUserPassword().equals(tbUserReq.getUserPassword())) {
//            return new DataInfo(1, "抱歉,用户名或密码错误", null, null);
//        }
//        if (user.getUserPassword().equals(tbUserReq.getUserPassword())) {
//            session.setAttribute("user", user);
//            return new DataInfo(0, "登录成功", null, user);
//        }
//        return null;
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(tbUserReq.getUserName(), tbUserReq.getUserPassword());
        try {
            subject.login(token);
        }catch (UnknownAccountException e){
            return new DataInfo(1, "抱歉,没有该用户", null, null);
        }catch (IncorrectCredentialsException e){
            return new DataInfo(1, "抱歉,用户名或密码错误", null, null);
        }
        return new DataInfo(0, "登录成功", null, tbUserReq);
    }

    @Override
    public DataInfo findAll(int page, int limit) {
//        PageRequest of = PageRequest.of(page - 1, limit);
        PageHelper.startPage(page, limit);
        List<TbUser> all = tbUserDao.findAll();
        PageInfo<TbUser> of = PageInfo.of(all);
        return new DataInfo(0, null, of.getTotal(), of.getList());
    }

    @Override
    public DataInfo add(TbUser tbUser) {
        tbUser.setCreateTime(new Date());
        tbUserDao.save(tbUser);
        return new DataInfo(0, "添加成功", null, null);
    }

    //删除用户
    @Override
    @Transactional
    public DataInfo delete(Integer id) {
        tbUserDao.deleteById(id);
        //删除用户和角色之间的联系
        tbUserRoleDao.deleteByUserid(id);
        return new DataInfo(0, "删除成功", null, null);
    }


}